package PlayerElement;

import static com.googlecode.charts4j.Color.*;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.Arrays;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.googlecode.charts4j.AxisLabels;
import com.googlecode.charts4j.AxisLabelsFactory;
import com.googlecode.charts4j.AxisStyle;
import com.googlecode.charts4j.AxisTextAlignment;
import com.googlecode.charts4j.Color;
import com.googlecode.charts4j.Data;
import com.googlecode.charts4j.GCharts;
import com.googlecode.charts4j.LineStyle;
import com.googlecode.charts4j.Plots;
import com.googlecode.charts4j.RadarChart;
import com.googlecode.charts4j.RadarPlot;
import com.googlecode.charts4j.RadialAxisLabels;
import com.googlecode.charts4j.Shape;


public class AnalysisReport {
	private BufferedImage image;					// Define RadarChart image
	private String player_name;						// Define game player name
	private double communication_performance;		// Define game player communication scores
	private double interaction_performance;			// Define game player interaction scores
	private double understanding_performance;		// Define game player understanding scroes
	
	/**
	 * Internal class for override JPanel class
	 * Import an image to JPanel
	 * @author Dr. Family
	 */
	private class ImagePanel extends JPanel {
		/**
		 * Constructor
		 * Read image from URL
		 * @param url
		 */
    	public ImagePanel(String url) {
    		super();
    	    try {               
    	      image = ImageIO.read(new URL(url));
    	    } catch (IOException e) {
    	      e.printStackTrace();
    	    } 
    	}
    	/**
    	 * Panel draw to screen
    	 */
    	public void paintComponent(Graphics g) {
    	    g.drawImage(image, 0, 0, null);
    	    repaint();
    	}
    }
	
	/**
	 * Constructor initialize class variables
	 * @param player_name
	 * @param performance
	 */
	public AnalysisReport(String player_name, int [] performance) {
		this.player_name = player_name;
		double total_performance = 0;
		for(int i = 0; i < performance.length; i++)
			total_performance += performance[i];
		this.communication_performance = (performance[0]/total_performance)*100;
		this.interaction_performance = (performance[1]/total_performance)*100;
		this.understanding_performance = (performance[2]/total_performance)*100;
	}
	
	/**
	 * Return generated radar chart according player performance 
	 * @return generated radar chart
	 */
	public RadarChart generateRadarChart() {
		RadarPlot plot = Plots.newRadarPlot(Data.newData(communication_performance, interaction_performance, understanding_performance, communication_performance));
        Color plotColor = Color.newColor("CC3366");
        plot.addShapeMarkers(Shape.SQUARE, plotColor, 12);
        plot.addShapeMarkers(Shape.SQUARE, WHITE, 8);
        plot.setColor(plotColor);
        plot.setLineStyle(LineStyle.newLineStyle(4, 1, 0));
        RadarChart chart = GCharts.newRadarChart(plot);
        chart.setTitle(player_name + " Performance", BLACK, 20);
        chart.setSize(400, 400);
        RadialAxisLabels radialAxisLabels = AxisLabelsFactory.newRadialAxisLabels("Communication", "Understanding", "Interaction");
        radialAxisLabels.setRadialAxisStyle(BLACK, 12);
        chart.addRadialAxisLabels(radialAxisLabels);
        AxisLabels contrentricAxisLabels = AxisLabelsFactory.newNumericAxisLabels(Arrays.asList(0, 20, 40, 60, 80, 100));
        contrentricAxisLabels.setAxisStyle(AxisStyle.newAxisStyle(BLACK, 12, AxisTextAlignment.RIGHT));
        chart.addConcentricAxisLabels(contrentricAxisLabels);
        return chart;
	}
	
	/**
	 * Return evaluation and suggestion message
	 * @return suggestion message to player
	 */
	public String evaluation() {
		String result = "<html>" + player_name + " needs to improve the following:\n";
		result += communication_eval();
		result += interaction_eval();
		result += understanding_eval();
		return result.replaceAll("\n", "<br>") + "</html>";
	}
	
	/**
	 * Return communication way suggestion message
	 * @return communication way suggestion message
	 */
	private String communication_eval() {
		String result = "";
		if ( communication_performance <= 20 )
			result += "-\t Talk with your parent more and more!\n";
		else if ( communication_performance <= 50 )
			result += "-\t Try to talk more!\n";
		return result;
	}
	
	/**
	 * Return interaction way suggestion message
	 * @return interaction way suggestion message
	 */
	private String interaction_eval() {
		String result = "";
		if ( interaction_performance <= 20 )
			result += "-\t You should have more activity with your family!\n";
		else if ( interaction_performance <= 50 )
			result += "-\t Try to have activity with your family!\n";
		return result;
	}
	
	/**
	 * Return understanding way suggestion message
	 * @return understanding way suggestion message
	 */
	private String understanding_eval() {
		String result = "";
		if ( understanding_performance <= 20 )
			result += "-\t Try to represent yourself and know more about your members!\n";
		else if ( understanding_performance <= 50 )
			result += "-\t Not enough understanding with your family members! try to know more about them!\n";
		return result;
	}
	
	/**
	 * Display the generated radar chart
	 * @param chart
	 * @return displayed radar chart or not
	 */
	public boolean getRadarChart(RadarChart chart) {
		String url = chart.toURLString();
		JFrame f = new JFrame("Window");
		JLabel label = new JLabel();
        label.setText(evaluation());
		f.add(new ImagePanel(url));
        f.add(label, BorderLayout.SOUTH);
        f.setSize(image.getWidth(), image.getHeight()+100);
        f.setVisible(true);
        return true;
	}
	
}